#!/bin/bash

FULLCOMMAND="$0 $@"
. ${BASEDIR}scripts/shflags

RID=$RANDOM

#define the flags
DEFINE_string 'robots' '1' 'The number of robots in the simulation' 'n'; # will be replaced by value in run_changedetection_baseline_script.sh
DEFINE_integer 'seed' '-1' 'Seed' 'x'; # will be replaced by value in run_changedetection_baseline_script.sh
DEFINE_string 'iterations' '1000000' 'Number of iterations' 'i' # will be replaced by value in run_changedetection_baseline_script.sh

DEFINE_string 'basedir' '/home/kvgelder/keesvangelder-changedetection/' 'Base dir of experiment' 'b'
DEFINE_string 'templatedir' 'scripts/templates/' 'd'
DEFINE_string 'confname' 'changedetection-advancedevolution' 'o'
DEFINE_string 'logdir' 'logs' 'g'

# Parse the flags
FLAGS "$@" || exit 1
eval set -- "${FlAGS_ARGV}"

BASEDIR=${FLAGS_basedir}
TEMPLATEDIR=${FLAGS_templatedir}
CONFNAME=${FLAGS_confname}

### copy the template configuration to the config dir, making the neccesary adjustments
# Determine some properties of the test
ROBOTCOUNT=${FLAGS_robots}
TIMESTAMP=`date "+%Y%m%d.%Hh%Mm%Ss"`
#LOGDIR=${FLAGS_logdir}
LOGDIR=$BASEDIR${FLAGS_logdir}
ESCAPEDLOGDIR=$(sed -e 's/[\/&]/\\&/g' <<< $LOGDIR)

# Determine where the configuration file will be placed
CONFDIR=${BASEDIR}config/
CONFFILE=${CONFDIR}${TIMESTAMP}.$RID.${TESTNAME}.$ROBOTCOUNT.properties
CONSOLELOGFILE=${BASEDIR}/logs/console-output.${TIMESTAMP}.$RID.log
ERRORLOGFILE=${BASEDIR}/logs/console-error.${TIMESTAMP}.$RID.log

OUTPUTLOGFILE=${ESCAPEDLOGDIR}\\/output.${TIMESTAMP}.$RID.log
GENOMELOGFILE=${ESCAPEDLOGDIR}\\/genomes.${TIMESTAMP}.$RID.log
ORGANISMSIZESLOGFILE=${ESCAPEDLOGDIR}\\/organism-sizes.${TIMESTAMP}.$RID.log
ORGANISMLOGFILE=${ESCAPEDLOGDIR}\\/organism.${TIMESTAMP}.$RID.log
LOGFILE=${ESCAPEDLOGDIR}\\/log.${TIMESTAMP}.$RID.txt

CLUSTERLOGFILE=${ESCAPEDLOGDIR}\\/clusterLog.${TIMESTAMP}.$RID.log
TRAVELLOGFILE=${ESCAPEDLOGDIR}\\/travelTrackLog.${TIMESTAMP}.$RID.log
LOCATIONLOGFILE=${ESCAPEDLOGDIR}\\/location.${TIMESTAMP}.$RID.log
#FITNESSLOGFILE=${ESCAPEDLOGDIR}\\/fitness.${TIMESTAMP}.$RID.log
#POPULATIONLOGFILE=${ESCAPEDLOGDIR}\\/population.${TIMESTAMP}.$RID.log

# Prepare the rules that will fill out the configuration file
SEEDREP=s/--RANDOMSEED/${FLAGS_seed:0:9}/g # extract only the first 9 decimals, because Roborobo can't handle int overflows
NBAGENTSREP=s/--NBAGENTS/${ROBOTCOUNT}/g
ITERATIONREP=s/--ITERATIONS/${FLAGS_iterations}/g

OUTPUTLOGREP=s/--OUTPUTLOG/${OUTPUTLOGFILE}/g
GENOMELOGREP=s/--GENOMELOG/${GENOMELOGFILE}/g
ORGANISMSIZESREP=s/--ORGANISMSIZESLOG/${ORGANISMSIZESLOGFILE}/g
ORGANISMREP=s/--ORGANISMSLOG/${ORGANISMLOGFILE}/g
LOGREP=s/--LOG/${LOGFILE}/g

CLUSTERLOGREP=s/--CLUSTERLOG/${CLUSTERLOGFILE}/g
TRAVELLOGREP=s/--TRAVELLOG/${TRAVELLOGFILE}/g
LOCATIONREP=s/--LOCATIONLOG/${LOCATIONLOGFILE}/g
#FITNESSLOGREP=s/--FITNESSLOG/${FITNESSLOGFILE}/g
#POPULATIONLOGREP=s/--POPULATIONLOG/${POPULATIONLOGFILE}/g

# Fill out and place the configuration file
sed -e $SEEDREP -e $NBAGENTSREP -e $OUTPUTLOGREP -e $GENOMELOGREP -e $ORGANISMSIZESREP -e $ORGANISMREP -e $LOGREP -e $ITERATIONREP -e $CLUSTERLOGREP -e $TRAVELLOGREP -e $LOCATIONREP ${BASEDIR}${TEMPLATEDIR}${CONFNAME}.template.properties > $CONFFILE

BINFILE=${BASEDIR}roborobo
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"/home/kvgelder/lib/lib"
$BINFILE -l $CONFFILE > $CONSOLELOGFILE 2> $ERRORLOGFILE
